/**
  ************************************* Copyright ******************************
  * FileName   : n32g45x_it.c
  * Version    : v1.0
  * Author     : yychao
  * Date       : 2023-10-10
  * Description:
  * Function List:
  	1. ....
  	   <version>:
  <modify staff>:
  		  <data>:
   <description>:
  	2. ...
  ******************************************************************************
 */
/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file n32g45x_it.c
 * @author Nations
 * @version v1.0.1
 *
 * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
 */
#include "n32g45x_it.h"
#include "n32g45x.h"
#include "main.h"
#include "gpio.h"
#include "usart.h"
#include "soft_timer.h"
#include "string.h"
#include "log.h"
#include "dma.h"
#include "usart.h"
#include "io_ctrl.h"
#include "screen.h"
#include <rtthread.h>
/** @addtogroup N32G45X_StdPeriph_Template
 * @{
 */

extern struct rt_messagequeue static_mq0;
/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
 * @brief  This function handles NMI exception.
 */
void NMI_Handler(void)
{
}
#if 0
/**
 * @brief  This function handles Hard Fault exception.
 */
void HardFault_Handler(void)
{
    /* Go to infinite loop when Hard Fault exception occurs */
    while (1)
    {
    }
}
#endif
/**
 * @brief  This function handles Memory Manage exception.
 */
void MemManage_Handler(void)
{
    /* Go to infinite loop when Memory Manage exception occurs */
    while (1)
    {
    }
}

/**
 * @brief  This function handles Bus Fault exception.
 */
void BusFault_Handler(void)
{
    /* Go to infinite loop when Bus Fault exception occurs */
    while (1)
    {
    }
}

/**
 * @brief  This function handles Usage Fault exception.
 */
void UsageFault_Handler(void)
{
    /* Go to infinite loop when Usage Fault exception occurs */
    while (1)
    {
    }
}

/**
 * @brief  This function handles SVCall exception.
 */
void SVC_Handler(void)
{
}

/**
 * @brief  This function handles Debug Monitor exception.
 */
void DebugMon_Handler(void)
{
}
#if 0
/**
 * @brief  This function handles SysTick Handler.
 */
void SysTick_Handler(void)
{

}
#endif
/**
 * @brief  This function handles DMA interrupt request defined in main.h .
 */
void DMA_IRQ_HANDLER(void)
{

}
void DMA1_Channel1_IRQHandler(void)
{
    if(DMA_GetIntStatus( DMA1_INT_TXC1, DMA1) != RESET) {
#if 0
        LOG_DEBUG("ONCE+0: ");
        for(uint8_t i = 0; i < 8; i++)
            LOG_DEBUG("%d ", ADCConvertedValue[i]);
        LOG_DEBUG("\r\n");
#endif
        DMA_ClrIntPendingBit(DMA1_INT_TXC1, DMA1);
        TIM_Enable(TIM2, DISABLE);

    }
}

void TIM1_UP_IRQHandler(void)
{

    if (TIM_GetIntStatus(TIM1, TIM_INT_UPDATE) != RESET)
    {
        TIM_ClrIntPendingBit(TIM1, TIM_INT_UPDATE);
        tickCnt_Update();
    }
}

void TIM6_IRQHandler(void)
{

    if (TIM_GetIntStatus(TIM6, TIM_INT_UPDATE) != RESET)
    {
        if(ctrl_state_st.time_sta == 1) {
            ctrl_state_st.time_cnt++;
            if(ctrl_state_st.time_cnt >= 1) {
                ctrl_state_st.time_chk = 1;
                ctrl_state_st.time_cnt= 0;
            }
        }

        if(ctrl_state_st.feed_start == 1) {
            ctrl_state_st.feed_start = 0;
        }
        ctrl_state_st.feed_cnt++;
        if(ctrl_state_st.clear_start == 1) {
            ctrl_state_st.clear_start = 0;
        }
        ctrl_state_st.clear_cnt++;
        TIM_ClrIntPendingBit(TIM6, TIM_INT_UPDATE);
    }

}
/**
 * @brief  This function handles USARTy global interrupt request.
 */
void USART1_IRQHandler(void)
{
    if (USART_GetIntStatus(USART1, USART_INT_RXDNE) != RESET) {
        if(usart1_cache.cnt < USART_CACHE_LEN) {
            usart1_cache.data[usart1_cache.cnt++] = USART_ReceiveData(USART1);
        } else {
            USART_ReceiveData(USART1);
        }
        USART_ClrIntPendingBit(USART1, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(USART1, USART_INT_IDLEF) != RESET) {
        USART_ReceiveData(USART1);
        USART_ClrIntPendingBit(USART1, USART_INT_IDLEF);
        softTimer_Start(TIMER1, MODE_ONE_SHOT, 15, TIM1_CB, NULL, 0);
        return;
#if 0
        usart_trans(USART1, &usart1_cache.data[1], usart1_cache.cnt - 1);
#else
        usart1_cache.data[0] = 1;
        usart1_cache.data[1] = usart1_cache.cnt >> 8;
        usart1_cache.data[2] = usart1_cache.cnt & 0x00ff;
        rt_mq_send(&static_mq0, &usart1_cache.data[0], USART_CACHE_LEN);
        memset(&usart1_cache, 0x00, sizeof(usart_cache));
#endif
        USART_ReceiveData(USART1);
        USART_ClrIntPendingBit(USART1, USART_INT_IDLEF);
        usart1_cache.cnt = 3;
    }
}
void USART2_IRQHandler(void)
{
    if (USART_GetIntStatus(USART2, USART_INT_RXDNE) != RESET) {
        if(usart2_cache.cnt < USART_CACHE_LEN) {
            usart2_cache.data[usart2_cache.cnt++] = USART_ReceiveData(USART2);
        } else {
            USART_ReceiveData(USART2);
        }
        USART_ClrIntPendingBit(USART2, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(USART2, USART_INT_IDLEF) != RESET) {
#if 0
        usart_trans(USART1, &usart1_cache.data[1], usart1_cache.cnt - 1);
#else
        usart2_cache.data[0] = 2;
        usart2_cache.data[1] = usart2_cache.cnt >> 8;
        usart2_cache.data[2] = usart2_cache.cnt & 0x00ff;
        rt_mq_send(&static_mq0, &usart2_cache.data[0], USART_CACHE_LEN);
        memset(&usart2_cache, 0x00, sizeof(usart_cache));
#endif
        USART_ReceiveData(USART2);
        USART_ClrIntPendingBit(USART2, USART_INT_IDLEF);
        usart2_cache.cnt = 3;
    }
}
void USART3_IRQHandler(void)
{
    if (USART_GetIntStatus(USART3, USART_INT_RXDNE) != RESET) {
        if(usart3_cache.cnt < USART_CACHE_LEN) {
            usart3_cache.data[usart3_cache.cnt++] = USART_ReceiveData(USART3);
        } else {
            USART_ReceiveData(USART3);
        }
        USART_ClrIntPendingBit(USART3, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(USART3, USART_INT_IDLEF) != RESET) {
#if 0
        usart_trans(USART1, &usart1_cache.data[1], usart1_cache.cnt - 1);
#else
        usart3_cache.data[0] = 3;
        usart3_cache.data[1] = usart3_cache.cnt >> 8;
        usart3_cache.data[2] = usart3_cache.cnt & 0x00ff;
        rt_mq_send(&static_mq0, &usart3_cache.data[0], USART_CACHE_LEN);
        memset(&usart3_cache, 0x00, sizeof(usart_cache));
#endif
        USART_ReceiveData(USART3);
        USART_ClrIntPendingBit(USART3, USART_INT_IDLEF);
        usart3_cache.cnt = 3;
    }
}
void UART4_IRQHandler(void)
{
    if (USART_GetIntStatus(UART4, USART_INT_RXDNE) != RESET) {
        if(usart4_cache.cnt < USART_CACHE_LEN) {
            usart4_cache.data[usart4_cache.cnt++] = USART_ReceiveData(UART4);
        } else {
            USART_ReceiveData(UART4);
        }
        USART_ClrIntPendingBit(UART4, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(UART4, USART_INT_IDLEF) != RESET) {
#if 0
        usart_trans(USART1, &usart1_cache.data[1], usart1_cache.cnt - 1);
#else
        usart4_cache.data[0] = 4;
        usart4_cache.data[1] = usart4_cache.cnt >> 8;
        usart4_cache.data[2] = usart4_cache.cnt & 0x00ff;
        rt_mq_send(&static_mq0, &usart4_cache.data[0], USART_CACHE_LEN);
        memset(&usart4_cache, 0x00, sizeof(usart_cache));
#endif
        USART_ReceiveData(UART4);
        USART_ClrIntPendingBit(UART4, USART_INT_IDLEF);
        usart4_cache.cnt = 3;
    }
}

void UART5_IRQHandler(void)
{
    if (USART_GetIntStatus(UART5, USART_INT_RXDNE) != RESET) {
        if(usart5_cache.cnt < USART_CACHE_LEN) {
            usart5_cache.data[usart5_cache.cnt++] = USART_ReceiveData(UART5);
        } else {
            USART_ReceiveData(UART5);
        }
        USART_ClrIntPendingBit(UART5, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(UART5, USART_INT_IDLEF) != RESET) {
#if 0
        usart_trans(USART1, &usart1_cache.data[1], usart1_cache.cnt - 1);
#else
        usart5_cache.data[0] = 5;
        usart5_cache.data[1] = usart5_cache.cnt >> 8;
        usart5_cache.data[2] = usart5_cache.cnt & 0x00ff;
        rt_mq_send(&static_mq0, &usart5_cache.data[0], USART_CACHE_LEN);
        memset(&usart5_cache, 0x00, sizeof(usart_cache));
#endif
        USART_ReceiveData(UART5);
        USART_ClrIntPendingBit(UART5, USART_INT_IDLEF);
        usart5_cache.cnt = 3;
    }
}

void UART6_IRQHandler(void)
{
    if (USART_GetIntStatus(UART6, USART_INT_RXDNE) != RESET) {
        if(usart6_cache.cnt < USART_CACHE_LEN) {
            usart6_cache.data[usart6_cache.cnt++] = USART_ReceiveData(UART6);
        } else {
            USART_ReceiveData(UART6);
        }
        USART_ClrIntPendingBit(UART6, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(UART6, USART_INT_IDLEF) != RESET) {
#if 0
        usart_trans(USART1, &usart1_cache.data[1], usart1_cache.cnt - 1);
#else
        usart6_cache.data[0] = 6;
        usart6_cache.data[1] = usart6_cache.cnt >> 8;
        usart6_cache.data[2] = usart6_cache.cnt & 0x00ff;
        rt_mq_send(&static_mq0, &usart6_cache.data[0], USART_CACHE_LEN);
        memset(&usart6_cache, 0x00, sizeof(usart_cache));
#endif
        USART_ReceiveData(UART6);
        USART_ClrIntPendingBit(UART6, USART_INT_IDLEF);
        usart6_cache.cnt = 3;
    }
}

void UART7_IRQHandler(void)
{
    if (USART_GetIntStatus(UART7, USART_INT_RXDNE) != RESET) {
        if(usart7_cache.cnt < USART_CACHE_LEN) {
            usart7_cache.data[usart7_cache.cnt++] = USART_ReceiveData(UART7);
        } else {
            USART_ReceiveData(UART7);
        }
        USART_ClrIntPendingBit(UART7, USART_INT_RXDNE);
    }
    if (USART_GetIntStatus(UART7, USART_INT_IDLEF) != RESET) {
#if 0
        usart_trans(USART1, &usart1_cache.data[1], usart1_cache.cnt - 1);
#else
        usart7_cache.data[0] = 7;
        usart7_cache.data[1] = usart7_cache.cnt >> 8;
        usart7_cache.data[2] = usart7_cache.cnt & 0x00ff;
        rt_mq_send(&static_mq0, &usart7_cache.data[0], USART_CACHE_LEN);
        memset(&usart7_cache, 0x00, sizeof(usart_cache));
#endif
        USART_ReceiveData(UART7);
        USART_ClrIntPendingBit(UART7, USART_INT_IDLEF);
        usart7_cache.cnt = 3;
    }
}
/******************************************************************************/
/*                 N32G45X Peripherals Interrupt Handlers                     */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_n32g45x.s).                                                 */
/******************************************************************************/

/**
 * @brief  This function handles PPP interrupt request.
 */
/*void PPP_IRQHandler(void)
{
}*/

/**
 * @}
 */
